package org.lookahead.parent.apidoc.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.*;

/**
 * 合作状态枚举，融合（学校合作状态+课件合作状态）
 */
@AllArgsConstructor
@Getter
@ToString
public enum CooperationTypeEnum {

    IN_COOPERATION(1, "合作中"),
    STOP(2, "停用"),
    EXPIRE(3, "过期"),
    NEAR_DUE(4, "即将到期"),
    OTHER(99, "其他");

    /**
     * 合作状态码
     */
    private final Integer code;
    /**
     * 合作类型名称
     */
    private final String message;

    private static final Map<Integer, CooperationTypeEnum> COOPERATION_TYPE_MAP = new HashMap<>();
    private static final List<CooperationTypeEnum> IN_COOPERATION_LIST = Arrays.asList(IN_COOPERATION, NEAR_DUE);

    static {
        for (CooperationTypeEnum typeEnum : CooperationTypeEnum.values()) {
            COOPERATION_TYPE_MAP.put(typeEnum.getCode(), typeEnum);
        }
    }

    /**
     * 获取合作code对应的枚举
     *
     * @param code 合作状态码
     * @return CooperationTypeEnum
     */
    public static CooperationTypeEnum getInstance(Integer code) {
        return COOPERATION_TYPE_MAP.getOrDefault(code, OTHER);
    }

    /**
     * 返回合作文案
     *
     * @param code 合作状态码
     * @return 合作文案
     */
    public static String getTypeMessage(Integer code) {
        return COOPERATION_TYPE_MAP.getOrDefault(code, OTHER).getMessage();
    }

    /**
     * 判断是否合作中
     *
     * @param code 合作状态码
     * @return boolean
     */
    public static boolean inCooperation(Integer code) {
        return IN_COOPERATION_LIST.contains(getInstance(code));
    }

    /**
     * 根据剩余时间获取合作枚举
     *
     * @param diffDays 剩余时间
     * @return CooperationTypeEnum
     */
    public static CooperationTypeEnum getCooperationType(Long diffDays) {
        if (diffDays >= 15L) {
            return IN_COOPERATION;
        } else if (diffDays >= 0L) {
            return NEAR_DUE;
        } else {
            return EXPIRE;
        }
    }
}
